import { observable, action, computed, toJS } from 'mobx';
import { WeaForm } from 'comsMobx';
import * as API_REPORT from '../../apis/reportApi';

import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
export class EChartSerie {
	/**
	 * unique id of this todo, immutable.
	 */

  id = '';
  name = '';
  type = 'bar';
  stack = getLabel('388005', '总量');
  label = {
		/* normal: {
				 show: true,
				 position: 'insideRight'
		 } */
  };
  data = [];
  dataKey = [];

  status = 0;
  itemStyle = {};

  @computed get asJson() {
    return {
      id: this.id,
      name: this.name,
      data: this.data,
      dataKey: this.dataKey,
      type: this.type,
      stack: this.stack,
      label: this.label,
      status: this.status,
      itemStyle: this.itemStyle,
    };
  }

	/**
	 * Update this todo with information from the server
	 */
  updateFromJson(json) {
    this.id = json.id;
    this.name = json.name;
    this.data = json.data;
    this.status = json.status;
    this.dataKey = json.dataKey;
    this.itemStyle = json.itemStyle;
  }

  dispose() {
  }
}

export class EChart {
  xAxis = new EChartAxis();
  yAxis = new EChartAxis();
  series = [];
  legend = new EChartLegend();
  tooltip = {};

  @computed get asJson() {
    return {
      xAxis: this.xAxis,
      yAxis: this.yAxis,
      series: this.series,
      legend: this.legend,
      tooltip: this.tooltip,
    };
  }

  updateFromJson(json) {
    this.xAxis = json.xAxis;
    this.yAxis = json.yAxis;
    this.series = json.series;
    this.legend = json.legend;
    this.tooltip = json.tooltip;
  }

  dispose() {
  }
}

export class EChartLegend {
  type = '';
  data = []

  @computed get asJson() {
    return {
      type: this.type,
      data: this.data,
    };
  }

  updateFromJson(json) {
    this.type = json.type;
    this.data = json.data;
  }

  dispose() {
  }
}

export class EChartAxis {
  type = '';
  data = [];
  axisLabel = {
    formatter(value) {
      if (value.length > 15) {
        return `${value.substring(0, 15)}...`;
      }
      return value;
    },
  };

  @computed get asJson() {
    return {
      type: this.type,
      data: this.data,
      axisLabel: this.axisLabel,
    };
  }

  updateFromJson(json) {
    this.type = json.type;
    this.data = json.data;
    if (json.axisLabel) {
      this.axisLabel = json.axisLabel;
    }
  }

  dispose() {
  }
}

/**
 * 公文报表  数据对象
 */
export default class ReportStore {
  // 当前选中tab
  @observable selectedTab = 0;
  @observable searchConfigFlow = [];
  @observable searchConfigDoc = [];
  @observable searchParamsFlow = {};
  @observable searchParamsDoc = {};
  @observable formFlow = new WeaForm();
  @observable formDoc = new WeaForm();
  @observable countDatas = [];
  @observable echart1 = {};
  @observable echart2 = {};
  @observable echart2title = '';
  @observable docChart1 = {};
  @observable docChart2 = {};
  @observable docChart3 = {};
  @observable docChart4 = {};
  @observable docChart5 = {};
  @observable docChart6 = {};
  @observable docChart7 = {};
  @observable docChart1title = '';
  @observable docChart2title = '';
  @observable docChart3title = '';
  @observable docChart4title = '';
  @observable docChart5title = '';
  @observable docChart6title = '';
  @observable docChart7title = '';
  @observable loading = false;
  @observable hasRight = false;

  @action.bound
  setSearchConfigFlow = (searchConfigFlow) => {
    this.searchConfigFlow = searchConfigFlow;
  }

  setSearchConfigDoc = (searchConfigDoc) => {
    this.searchConfigDoc = searchConfigDoc;
  }

  setSelectedTab = (selectedTab) => {
    this.selectedTab = selectedTab;
  }

  @action
  setCountDatas = (countDatas) => {
    this.countDatas = countDatas;
  }

  @action.bound
  changeSelectedTab(v) {
    this.setSelectedTab(v);
    this.buildSearchCondition();
  }

  @action.bound
  buildSearchCondition() {
    if (this.selectedTab == 0) {
      API_REPORT.getOdocReportSearchCondition({}).then((data) => {
        this.hasRight = data.condition[0].hasRight == true;
        if (this.hasRight) {
          this.setSearchConfigFlow(data.condition[0].config.items);
          let conditions = [];
          conditions.push(data.condition[0].config);
          this.formFlow.initFormFields(conditions);
          this.doSearch(this.formFlow.getFormParams());
        }
      });
    } else if (this.selectedTab == 1) {
      API_REPORT.getOdocDocReportSearchCondition({}).then((data) => {
        this.hasRight = data.hasRight != false;
        if (this.hasRight) {
          this.setSearchConfigDoc(data.condition[0].config.items);
          let conditions = [];
          conditions.push(data.condition[0].config);
          this.formDoc.initFormFields(conditions);
          this.doSearch(this.formDoc.getFormParams());
        }
      });
    } else if (this.selectedTab == 2) { }
  }

  @action.bound
  doSearch(params) {
    this.loading = true;
    if (this.selectedTab == 0) {
      this.searchParamsFlow = this.formFlow.getFormParams();
      Object.assign(this.searchParamsFlow, this.searchParamsFlow, params);
      if (params.officaltype == '1') {
        this.echart2title = getLabel('383721', '发文环节统计');
      } else if (params.officaltype == '2') {
        this.echart2title = getLabel('510617', '收文环节统计');
      } else if (params.officaltype == '3') {
        this.echart2title = getLabel('510618', '签报环节统计');
      } else {
        this.echart2title = '';
      }
      API_REPORT.getOdocReportCountData(toJS(this.searchParamsFlow)).then((data) => {
        this.buildCountData(data);
      });

      API_REPORT.getOdocReportEchartData(toJS(this.searchParamsFlow)).then((data) => {
        this.loading = false;
        this.buildFlowChart1(data);
        this.buildFlowChart2(data);
      });
    } else if (this.selectedTab == 1) {
      this.searchParamsDoc = this.formDoc.getFormParams();
      Object.assign(this.searchParamsDoc, this.searchParamsDoc, params);
      API_REPORT.getOdocDocReportEchartData(toJS(this.searchParamsDoc)).then((data) => {
        this.loading = false;
        this.buildDocChart1(data);
        this.buildDocChart2(data);
        this.buildDocChart3(data);
        this.buildDocChart4(data);
        this.buildDocChart5(data);
      });
    }
  }

  buildDocChart3(data) {
    this.docChart3title = data.echart3title;
    // let xAxisData = [];
    let seriesData = [];
    let seriesDataKey = [];
    data.echart3.datas.forEach((element, index) => {
      // xAxisData.push(data.echart3[index].title);
      seriesData.push(data.echart3.datas[index].value);
      seriesDataKey.push(data.echart3.datas[index].key);
    });

    let series = [];
    let eChartSerie = new EChartSerie();
    let serie = {
      name: '1',
      type: 'bar',
      data: seriesData,
      dataKey: seriesDataKey,
    };
    eChartSerie.updateFromJson(serie);
    series.push(eChartSerie);


    let xAxis = new EChartAxis();
    xAxis.updateFromJson(
      { type: 'category', data: data.echart3.xAxisData },
    );
    let yAxis = new EChartAxis();
    yAxis.updateFromJson(
      { type: 'value', data: [] },
    );

    let docChart3 = new EChart();
    docChart3.updateFromJson(
      {
        xAxis,
        yAxis,
        series,
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
          formatter(params) {
            let tar = params[0];
            if (tar) {
              return `${tar.name} : ${tar.value}`;
            }
          },
        },
      },
    );

    this.docChart3 = docChart3;
  }

  buildDocChart2(data) {
    this.docChart2title = data.echart2title;

    let yAxisData = [];
    let seriesData = [];
    let seriesDataKey = [];
    data.echart2.forEach((element, index) => {
      yAxisData.push(data.echart2[index].title);
      seriesData.push(data.echart2[index].value);
      seriesDataKey.push(data.echart2[index].key);
    });


    let xAxis = new EChartAxis();
    xAxis.updateFromJson(
      { type: 'value', data: [] },
    );
    let yAxis = new EChartAxis();
    yAxis.updateFromJson(
      { type: 'category', data: yAxisData },
    );
    let series = [];
    let eChartSerie = new EChartSerie();
    let serie = {
      name: '1',
      type: 'bar',
      data: seriesData,
      dataKey: seriesDataKey,
    };
    eChartSerie.updateFromJson(serie);
    series.push(eChartSerie);

    let docChart2 = new EChart();
    docChart2.updateFromJson(
      {
        xAxis,
        yAxis,
        series,
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
          formatter(params) {
            let tar = params[0];
            if (tar) {
              return `${tar.name} : ${tar.value}`;
            }
          },
        },
      },
    );

    this.docChart2 = docChart2;
  }

  buildDocChart1(data) {
    this.docChart1title = data.echart1title;

    let yAxisData = [];
    let seriesData = [];
    let seriesDataKey = [];
    data.echart1.forEach((element, index) => {
      yAxisData.push(data.echart1[index].title);
      seriesData.push(data.echart1[index].value);
      seriesDataKey.push(data.echart1[index].key);
    });


    let xAxis = new EChartAxis();
    xAxis.updateFromJson(
      { type: 'value', data: [] },
    );
    let yAxis = new EChartAxis();
    yAxis.updateFromJson(
      { type: 'category', data: yAxisData },
    );
    let series = [];
    let eChartSerie = new EChartSerie();
    let serie = {
      name: '1',
      type: 'bar',
      data: seriesData,
      dataKey: seriesDataKey,
    };
    eChartSerie.updateFromJson(serie);
    series.push(eChartSerie);

    let docChart1 = new EChart();
    docChart1.updateFromJson(
      {
        xAxis,
        yAxis,
        series,
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
          formatter(params) {
            let tar = params[0];
            if (tar) {
              return `${tar.name} : ${tar.value}`;
            }
          },
        },
      },
    );

    this.docChart1 = docChart1;
  }

  buildDocChart4(data) {
    this.docChart4title = data.echart4title;

    let yAxisData = [];
    let seriesData = [];
    let seriesDataKey = [];
    data.echart4.forEach((element, index) => {
      yAxisData.push(data.echart4[index].title);
      seriesData.push(data.echart4[index].value);
      seriesDataKey.push(data.echart4[index].key);
    });


    let xAxis = new EChartAxis();
    xAxis.updateFromJson(
      { type: 'value', data: [] },
    );
    let yAxis = new EChartAxis();
    yAxis.updateFromJson(
      { type: 'category', data: yAxisData },
    );
    let series = [];
    let eChartSerie = new EChartSerie();
    let serie = {
      name: '1',
      type: 'bar',
      data: seriesData,
      dataKey: seriesDataKey,
    };
    eChartSerie.updateFromJson(serie);
    series.push(eChartSerie);

    let docChart4 = new EChart();
    docChart4.updateFromJson(
      {
        xAxis,
        yAxis,
        series,
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
          formatter(params) {
            let tar = params[0];
            if (tar) {
              return `${tar.name} : ${tar.value}`;
            }
          },
        },
      },
    );

    this.docChart4 = docChart4;
  }

  buildDocChart5(data) {
    this.docChart5title = data.echart5title;

    let yAxisData = [];
    let seriesData = [];
    let seriesDataKey = [];
    data.echart5.forEach((element, index) => {
      yAxisData.push(data.echart5[index].title);
      seriesData.push(data.echart5[index].value);
      seriesDataKey.push(data.echart5[index].key);
    });


    let xAxis = new EChartAxis();
    xAxis.updateFromJson(
      { type: 'value', data: [] },
    );
    let yAxis = new EChartAxis();
    yAxis.updateFromJson(
      { type: 'category', data: yAxisData },
    );
    let series = [];
    let eChartSerie = new EChartSerie();
    let serie = {
      name: '1',
      type: 'bar',
      data: seriesData,
      dataKey: seriesDataKey,
    };
    eChartSerie.updateFromJson(serie);
    series.push(eChartSerie);

    let docChart5 = new EChart();
    docChart5.updateFromJson(
      {
        xAxis,
        yAxis,
        series,
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
          formatter(params) {
            let tar = params[0];
            if (tar) {
              return `${tar.name} : ${tar.value}`;
            }
          },
        },
      },
    );

    this.docChart5 = docChart5;
  }

  buildFlowChart2(data) {
    let xAxisData = [];
    let seriesData = [];
    let seriesDataKey = [];
    data.echart2.forEach((element, index) => {
      xAxisData.push(data.echart2[index].title);
      seriesData.push(data.echart2[index].value);
      seriesDataKey.push(data.echart2[index].key);
    });

    let series = [];
    let eChartSerie = new EChartSerie();
    let serie = {
      name: '1',
      type: 'bar',
      data: seriesData,
      dataKey: seriesDataKey,
    };
    eChartSerie.updateFromJson(serie);
    series.push(eChartSerie);


    let xAxis = new EChartAxis();
    xAxis.updateFromJson(
      { type: 'category', data: xAxisData },
    );
    let yAxis = new EChartAxis();
    yAxis.updateFromJson(
      { type: 'value', data: [] },
    );

    let echart2 = new EChart();
    echart2.updateFromJson(
      {
        xAxis,
        yAxis,
        series,
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
          formatter(params) {
            let tar = params[0];
            if (tar) {
              return `${tar.name} : ${tar.value}`;
            }
          },
        },
      },
    );

    this.echart2 = echart2;
  }

  buildFlowChart1(data) {
    let xAxis = new EChartAxis();
    xAxis.updateFromJson(
      { type: 'value', data: [] },
    );
    let yAxis = new EChartAxis();
    yAxis.updateFromJson(
      { type: 'category', data: data.yAxisData },
    );

    // 构建流程统计chart1数据
    let flowReqData = [];
    let flowReqDataKey = [];
    data.yAxisData.forEach((element, index) => {
      flowReqData[index] = Number.parseInt(data.flowReqData[index].value);
      flowReqDataKey[index] = data.flowReqData[index].key;
    });

    let endReqData = [];
    let endReqDataKey = [];
    data.yAxisData.forEach((element, index) => {
      endReqData[index] = Number.parseInt(data.endReqData[index].value);
      endReqDataKey[index] = data.endReqData[index].key;
    });

    let series = [];
    data.legend.forEach((element, index) => {
      if (data.legend[index] == getLabel('23773', '流转中')) {
        let eChartSerie = new EChartSerie();
        let serie = {
          name: data.legend[index],
          data: flowReqData,
          status: 2,
          dataKey: flowReqDataKey,
          itemStyle: {
            color: '#F67C4C',
          },
        };
        eChartSerie.updateFromJson(serie);
        series.push(eChartSerie);
      }
      if (data.legend[index] == getLabel('251', '归档')) {
        let eChartSerie = new EChartSerie();
        let serie = {
          name: data.legend[index],
          data: endReqData,
          status: 1,
          dataKey: endReqDataKey,
          itemStyle: {
            color: '#D65DF4',
          },
        };
        eChartSerie.updateFromJson(serie);
        series.push(eChartSerie);
      }
    });

    let legend = new EChartLegend();
    legend.updateFromJson({ data: data.legend });

    let echart1 = new EChart();
    echart1.updateFromJson(
      {
        xAxis,
        yAxis,
        series,
        legend,
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
      },
    );

    this.echart1 = echart1;
  }

  buildCountData(data) {
    data.countData.forEach((element) => {
      if (element.isUp == 1) {
        element.isUpClass = 'report-count-span-green';
      } else {
        element.isUpClass = 'report-count-span-red';
      }
    });
    this.setCountDatas(data.countData);
  }

  @action.bound
  setMobxStatus(params = {}) {
    Object.keys(params).map((key) => {
      this[key] = params[key];
    });
  }
}

